﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using CollegeShop.Api.DBUtility;//Please add references
namespace CollegeShop.Api.Infrastructure
{
	/// <summary>
	/// 数据访问类:t_order
	/// </summary>
	public partial class t_order
	{
		public t_order()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId(int college)
		{
            return DbHelperMySQL.GetMaxID(college, "ID", "t_order"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
        public bool Exists(int college, int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from t_order");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;

            return DbHelperMySQL.Exists(college, strSql.ToString(), parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
        public bool Add(int college, CollegeShop.Api.Entity.t_order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into t_order(");
			strSql.Append("OrderNumber,TotalAmount,TotalMoney,BuyUser,LinkMan,LinkPhone,DormitoryID,DeliveryAddress,GoodsDescript,Payway,Remark,OrderTime,OrderClient,State,HandleUser,SalesUnitID,HandleTime,BuyWay,BuyClient,AnonymousID)");
			strSql.Append(" values (");
			strSql.Append("@OrderNumber,@TotalAmount,@TotalMoney,@BuyUser,@LinkMan,@LinkPhone,@DormitoryID,@DeliveryAddress,@GoodsDescript,@Payway,@Remark,@OrderTime,@OrderClient,@State,@HandleUser,@SalesUnitID,@HandleTime,@BuyWay,@BuyClient,@AnonymousID)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@OrderNumber", MySqlDbType.VarChar,50),
					new MySqlParameter("@TotalAmount", MySqlDbType.Int32,11),
					new MySqlParameter("@TotalMoney", MySqlDbType.Decimal,18),
					new MySqlParameter("@BuyUser", MySqlDbType.VarChar,100),
					new MySqlParameter("@LinkMan", MySqlDbType.VarChar,30),
					new MySqlParameter("@LinkPhone", MySqlDbType.VarChar,30),
					new MySqlParameter("@DormitoryID", MySqlDbType.Int32,11),
					new MySqlParameter("@DeliveryAddress", MySqlDbType.VarChar,200),
					new MySqlParameter("@GoodsDescript", MySqlDbType.VarChar,1),
					new MySqlParameter("@Payway", MySqlDbType.Int32,11),
					new MySqlParameter("@Remark", MySqlDbType.VarChar,512),
					new MySqlParameter("@OrderTime", MySqlDbType.DateTime),
					new MySqlParameter("@OrderClient", MySqlDbType.Int32,11),
					new MySqlParameter("@State", MySqlDbType.Int32,11),
					new MySqlParameter("@HandleUser", MySqlDbType.VarChar,100),
					new MySqlParameter("@SalesUnitID", MySqlDbType.Int32,11),
					new MySqlParameter("@HandleTime", MySqlDbType.DateTime),
					new MySqlParameter("@BuyWay", MySqlDbType.Int32,11),
					new MySqlParameter("@BuyClient", MySqlDbType.Int32,11),
					new MySqlParameter("@AnonymousID", MySqlDbType.Int32,11)};
			parameters[0].Value = model.OrderNumber;
			parameters[1].Value = model.TotalAmount;
			parameters[2].Value = model.TotalMoney;
			parameters[3].Value = model.BuyUser;
			parameters[4].Value = model.LinkMan;
			parameters[5].Value = model.LinkPhone;
			parameters[6].Value = model.DormitoryID;
			parameters[7].Value = model.DeliveryAddress;
			parameters[8].Value = model.GoodsDescript;
			parameters[9].Value = model.Payway;
			parameters[10].Value = model.Remark;
			parameters[11].Value = model.OrderTime;
			parameters[12].Value = model.OrderClient;
			parameters[13].Value = model.State;
			parameters[14].Value = model.HandleUser;
			parameters[15].Value = model.SalesUnitID;
			parameters[16].Value = model.HandleTime;
			parameters[17].Value = model.BuyWay;
			parameters[18].Value = model.BuyClient;
			parameters[19].Value = model.AnonymousID;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
        public bool Update(int college, CollegeShop.Api.Entity.t_order model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_order set ");
			strSql.Append("OrderNumber=@OrderNumber,");
			strSql.Append("TotalAmount=@TotalAmount,");
			strSql.Append("TotalMoney=@TotalMoney,");
			strSql.Append("BuyUser=@BuyUser,");
			strSql.Append("LinkMan=@LinkMan,");
			strSql.Append("LinkPhone=@LinkPhone,");
			strSql.Append("DormitoryID=@DormitoryID,");
			strSql.Append("DeliveryAddress=@DeliveryAddress,");
			strSql.Append("GoodsDescript=@GoodsDescript,");
			strSql.Append("Payway=@Payway,");
			strSql.Append("Remark=@Remark,");
			strSql.Append("OrderTime=@OrderTime,");
			strSql.Append("OrderClient=@OrderClient,");
			strSql.Append("State=@State,");
			strSql.Append("HandleUser=@HandleUser,");
			strSql.Append("SalesUnitID=@SalesUnitID,");
			strSql.Append("HandleTime=@HandleTime,");
			strSql.Append("BuyWay=@BuyWay,");
			strSql.Append("BuyClient=@BuyClient,");
			strSql.Append("AnonymousID=@AnonymousID");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@OrderNumber", MySqlDbType.VarChar,50),
					new MySqlParameter("@TotalAmount", MySqlDbType.Int32,11),
					new MySqlParameter("@TotalMoney", MySqlDbType.Decimal,18),
					new MySqlParameter("@BuyUser", MySqlDbType.VarChar,100),
					new MySqlParameter("@LinkMan", MySqlDbType.VarChar,30),
					new MySqlParameter("@LinkPhone", MySqlDbType.VarChar,30),
					new MySqlParameter("@DormitoryID", MySqlDbType.Int32,11),
					new MySqlParameter("@DeliveryAddress", MySqlDbType.VarChar,200),
					new MySqlParameter("@GoodsDescript", MySqlDbType.VarChar,1),
					new MySqlParameter("@Payway", MySqlDbType.Int32,11),
					new MySqlParameter("@Remark", MySqlDbType.VarChar,512),
					new MySqlParameter("@OrderTime", MySqlDbType.DateTime),
					new MySqlParameter("@OrderClient", MySqlDbType.Int32,11),
					new MySqlParameter("@State", MySqlDbType.Int32,11),
					new MySqlParameter("@HandleUser", MySqlDbType.VarChar,100),
					new MySqlParameter("@SalesUnitID", MySqlDbType.Int32,11),
					new MySqlParameter("@HandleTime", MySqlDbType.DateTime),
					new MySqlParameter("@BuyWay", MySqlDbType.Int32,11),
					new MySqlParameter("@BuyClient", MySqlDbType.Int32,11),
					new MySqlParameter("@AnonymousID", MySqlDbType.Int32,11),
					new MySqlParameter("@ID", MySqlDbType.Int32,11)};
			parameters[0].Value = model.OrderNumber;
			parameters[1].Value = model.TotalAmount;
			parameters[2].Value = model.TotalMoney;
			parameters[3].Value = model.BuyUser;
			parameters[4].Value = model.LinkMan;
			parameters[5].Value = model.LinkPhone;
			parameters[6].Value = model.DormitoryID;
			parameters[7].Value = model.DeliveryAddress;
			parameters[8].Value = model.GoodsDescript;
			parameters[9].Value = model.Payway;
			parameters[10].Value = model.Remark;
			parameters[11].Value = model.OrderTime;
			parameters[12].Value = model.OrderClient;
			parameters[13].Value = model.State;
			parameters[14].Value = model.HandleUser;
			parameters[15].Value = model.SalesUnitID;
			parameters[16].Value = model.HandleTime;
			parameters[17].Value = model.BuyWay;
			parameters[18].Value = model.BuyClient;
			parameters[19].Value = model.AnonymousID;
			parameters[20].Value = model.ID;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public bool Delete(int college, int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_order ");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;

            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
        public bool DeleteList(int college, string IDlist)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from t_order ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(college, strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public CollegeShop.Api.Entity.t_order GetModel(int college, int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,OrderNumber,TotalAmount,TotalMoney,BuyUser,LinkMan,LinkPhone,DormitoryID,DeliveryAddress,GoodsDescript,Payway,Remark,OrderTime,OrderClient,State,HandleUser,SalesUnitID,HandleTime,BuyWay,BuyClient,AnonymousID from t_order ");
			strSql.Append(" where ID=@ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ID", MySqlDbType.Int32)
			};
			parameters[0].Value = ID;

			CollegeShop.Api.Entity.t_order model=new CollegeShop.Api.Entity.t_order();
            DataSet ds = DbHelperMySQL.Query(college, strSql.ToString(), parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public CollegeShop.Api.Entity.t_order DataRowToModel(DataRow row)
		{
			CollegeShop.Api.Entity.t_order model=new CollegeShop.Api.Entity.t_order();
			if (row != null)
			{
				if(row["ID"]!=null && row["ID"].ToString()!="")
				{
					model.ID=int.Parse(row["ID"].ToString());
				}
				if(row["OrderNumber"]!=null)
				{
					model.OrderNumber=row["OrderNumber"].ToString();
				}
				if(row["TotalAmount"]!=null && row["TotalAmount"].ToString()!="")
				{
					model.TotalAmount=int.Parse(row["TotalAmount"].ToString());
				}
				if(row["TotalMoney"]!=null && row["TotalMoney"].ToString()!="")
				{
					model.TotalMoney=decimal.Parse(row["TotalMoney"].ToString());
				}
				if(row["BuyUser"]!=null)
				{
					model.BuyUser=row["BuyUser"].ToString();
				}
				if(row["LinkMan"]!=null)
				{
					model.LinkMan=row["LinkMan"].ToString();
				}
				if(row["LinkPhone"]!=null)
				{
					model.LinkPhone=row["LinkPhone"].ToString();
				}
				if(row["DormitoryID"]!=null && row["DormitoryID"].ToString()!="")
				{
					model.DormitoryID=int.Parse(row["DormitoryID"].ToString());
				}
				if(row["DeliveryAddress"]!=null)
				{
					model.DeliveryAddress=row["DeliveryAddress"].ToString();
				}
				if(row["GoodsDescript"]!=null)
				{
					model.GoodsDescript=row["GoodsDescript"].ToString();
				}
				if(row["Payway"]!=null && row["Payway"].ToString()!="")
				{
					model.Payway=int.Parse(row["Payway"].ToString());
				}
				if(row["Remark"]!=null)
				{
					model.Remark=row["Remark"].ToString();
				}
				if(row["OrderTime"]!=null && row["OrderTime"].ToString()!="")
				{
					model.OrderTime=DateTime.Parse(row["OrderTime"].ToString());
				}
				if(row["OrderClient"]!=null && row["OrderClient"].ToString()!="")
				{
					model.OrderClient=int.Parse(row["OrderClient"].ToString());
				}
				if(row["State"]!=null && row["State"].ToString()!="")
				{
					model.State=int.Parse(row["State"].ToString());
				}
				if(row["HandleUser"]!=null)
				{
					model.HandleUser=row["HandleUser"].ToString();
				}
				if(row["SalesUnitID"]!=null && row["SalesUnitID"].ToString()!="")
				{
					model.SalesUnitID=int.Parse(row["SalesUnitID"].ToString());
				}
				if(row["HandleTime"]!=null && row["HandleTime"].ToString()!="")
				{
					model.HandleTime=DateTime.Parse(row["HandleTime"].ToString());
				}
				if(row["BuyWay"]!=null && row["BuyWay"].ToString()!="")
				{
					model.BuyWay=int.Parse(row["BuyWay"].ToString());
				}
				if(row["BuyClient"]!=null && row["BuyClient"].ToString()!="")
				{
					model.BuyClient=int.Parse(row["BuyClient"].ToString());
				}
				if(row["AnonymousID"]!=null && row["AnonymousID"].ToString()!="")
				{
					model.AnonymousID=int.Parse(row["AnonymousID"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
        public DataSet GetList(int college, string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,OrderNumber,TotalAmount,TotalMoney,BuyUser,LinkMan,LinkPhone,DormitoryID,DeliveryAddress,GoodsDescript,Payway,Remark,OrderTime,OrderClient,State,HandleUser,SalesUnitID,HandleTime,BuyWay,BuyClient,AnonymousID ");
			strSql.Append(" FROM t_order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            return DbHelperMySQL.Query(college, strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
        public int GetRecordCount(int college, string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM t_order ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperMySQL.GetSingle(college, strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
        public DataSet GetListByPage(int college, string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from t_order T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(college, strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "t_order";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

